refactor(sqlite): optimize vector-search, shard-manager, transcript-manager#12
Merged
Conversation
…zation, token count fix
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
src/services/sqlite/vector-search.ts (1)
459-464: ⚡ Quick winAvoid per-comparison allocations in Jaccard calculation.
Line 462 and Line 463 still allocate intermediate arrays/sets inside the O(n²) loop. You can compute intersection/union directly from set sizes.
♻️ Proposed allocation-free Jaccard inner loop
- const intersectionSize = [...candidateWords].filter((w) => selectedWords.has(w)).length; - const unionSize = new Set([...candidateWords, ...selectedWords]).size; + let intersectionSize = 0; + const [smaller, larger] = + candidateWords.size <= selectedWords.size + ? [candidateWords, selectedWords] + : [selectedWords, candidateWords]; + for (const w of smaller) { + if (larger.has(w)) intersectionSize++; + } + const unionSize = candidateWords.size + selectedWords.size - intersectionSize; const jaccard = unionSize > 0 ? intersectionSize / unionSize : 0;🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/services/sqlite/vector-search.ts` around lines 459 - 464, The Jaccard calculation inside the loop (using candidateWords, selectedWords, getWordSet, finalResults, and jaccard) currently allocates temporary arrays/sets via [...candidateWords].filter(...) and new Set([...candidateWords, ...selectedWords]) on every comparison; replace this with an allocation-free approach: iterate the smaller Set (candidateWords or selectedWords) and count intersectionSize by checking set.has(item), then compute unionSize as candidateWords.size + selectedWords.size - intersectionSize and compute jaccard = unionSize > 0 ? intersectionSize / unionSize : 0; update the inner loop to use this method to avoid per-comparison allocations.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/services/sqlite/transcript-manager.ts`:
- Around line 27-33: The current loop counts every whitespace character which
overestimates tokens; instead detect token boundaries by incrementing count only
when you transition from a whitespace to a non-whitespace character. In the
function using variables text, count and ch (the block that currently loops over
text and returns Math.ceil((count + 1) * 1.33)), track previousCharIsSpace (or
prevIsSpace) and only increment count when prevIsSpace is true and current ch is
non-whitespace; initialize prevIsSpace = true to count the first token correctly
and keep the final Math.ceil scaling unchanged.
---
Nitpick comments:
In `@src/services/sqlite/vector-search.ts`:
- Around line 459-464: The Jaccard calculation inside the loop (using
candidateWords, selectedWords, getWordSet, finalResults, and jaccard) currently
allocates temporary arrays/sets via [...candidateWords].filter(...) and new
Set([...candidateWords, ...selectedWords]) on every comparison; replace this
with an allocation-free approach: iterate the smaller Set (candidateWords or
selectedWords) and count intersectionSize by checking set.has(item), then
compute unionSize as candidateWords.size + selectedWords.size - intersectionSize
and compute jaccard = unionSize > 0 ? intersectionSize / unionSize : 0; update
the inner loop to use this method to avoid per-comparison allocations.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: 0d7a1f59-6fd1-49b7-a9ad-ca733152fcbe
📒 Files selected for processing (3)
src/services/sqlite/shard-manager.tssrc/services/sqlite/transcript-manager.tssrc/services/sqlite/vector-search.ts
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/services/sqlite/vector-search.ts`:
- Around line 441-453: getWordSet currently tokenises on whitespace so words
with punctuation (e.g. "important.") remain distinct; update getWordSet to
extract alphabetic word runs instead (use a word-boundary/letter regex like
match(/[a-z]+/g) after toLowerCase) and fall back to an empty array when match
returns null before building the Set; keep using wordSetCache and the same cache
key but ensure you cache the normalized set built from the regex tokens to fix
the Jaccard undercounting.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: 7a9ad570-09e5-4133-9eb4-e711f3f593d8
📒 Files selected for processing (2)
src/services/sqlite/transcript-manager.tssrc/services/sqlite/vector-search.ts
🚧 Files skipped from review as they are similar to previous changes (1)
- src/services/sqlite/transcript-manager.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Changes
VectorSearch: AddedWeakMap-based statement cache per database handle. Eliminates re-preparation overhead forinsertVector,deleteVector,updateVector,listMemories,getAllMemories,getMemoryById,countVectors.ShardManagerprepared statements to constructor-initialized cached properties (activeShardStmt,allShardsStmt,scopedShardsStmt,createShardStmt).Mapcache insearchAcrossShards, avoiding repeated string splitting in the O(n²) comparison loop.split()inapproximateTokenCountwith a char-code scan loop — faster and avoids regex compilation overhead.getAllShards: Removed duplicated SQL preparation logic, unified to cached statements with ternary dispatch.Impact
split()Testing
Requesting @coderabbitai full review for additional optimization opportunities in the SQLite/vector layer and across the codebase.
Summary by CodeRabbit